<template>
    <div>
        <table class="table">
            <tbody class="table-success">
                <tr>
                    <td>户型名称</td>
                    <td>
                        <input type="text" v-model="data.hname">
                    </td>
                </tr>
                <tr>
                    <td>套内面积</td>
                    <td>
                        <input type="text" v-model="data.houseArea">
                    </td>
                </tr>
                <tr>
                    <td>建筑面积</td>
                    <td>
                        <input type="text" v-model="data.buildArea">
                    </td>
                </tr>
                <tr>
                    <td>户型图</td>
                    <td>
                        <input type="file" @change="Up">
                        <img :src="'https://localhost:7231'+data.houseImg" style="width: 80px;height: 80px;">
                    </td>
                </tr>
                <tr>
                    <td>结构类型</td>
                    <td>
                        <select v-model="data.tid">
                            <option :value="0">==请选择==</option>
                            <option v-for="a in showdata" :value="a.tid">{{a.tname}}</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td></td>
                    <td>
                        <button class="btn btn-outline-primary"  @click="add">修改</button>
                        <button class="btn btn-outline-primary"  @click="back">取消修改</button>
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
</template>

<script setup lang="ts">
import axios from 'axios';
import { ref,onMounted } from 'vue';
import { useRoute, useRouter } from 'vue-router';

onMounted(() => {
    showdd();
    backdata();
})

const route=useRoute();
const backdata=()=>{
    var sid= route.query.id;
    axios.get("https://localhost:7231/api/House/GetHouseById",{params:{id:sid}}).then(res=>{
        data.value=res.data;
    })
}

const Up=(e:any)=>{
    var f=e.target.files[0];
    var fd=new FormData();
    fd.append("files",f);
    if(f.type!="image/png"&&f.type!="image/jpeg"){
        alert("图片只支持PNG、JPG格式");
        return;
    }
    if(f.size>1*1024*1024){
        alert("图片大小最多不能超过1MB");
        return;
    }
    axios.post("https://localhost:7231/api/Upload/Up",fd).then(res=>{
        data.value.houseImg=res.data;
    })
}


const showdata=ref([{
    "tid": 0,
    "tname": ""
}])

const showdd=()=>{
    axios.get("https://localhost:7231/api/House/GetHouseTypes").then(res=>{
        showdata.value=res.data;
    })
}

const data=ref({
    "hid": 0,
  "hname": "",
  "houseArea": 0,
  "buildArea": 0,
  "houseImg": "",
  "tid": 0,
  "isDel": true
})

const router=useRouter();

const add=()=>{
    var a=60;
    var b=300;
    if(data.value.hname==""){
        alert("户型名称不能为空");
        return;
    }
    if(data.value.houseArea==0){
        alert("套内面积不能为空");
        return;
    }
    if(data.value.buildArea==0){
        alert("建筑面积不能为空");
        return;
    }
    if(data.value.houseArea>=data.value.buildArea){
        alert("套内面积必须小于建筑面积");
        return;
    }
    if(data.value.tid==0){
        alert("结构类型必须选择");
        return;
    }
    
    if(data.value.buildArea<a){
        alert("建筑面积必须在60——300之间");
        return;
    }
    if(data.value.buildArea>b){
        alert("建筑面积必须在60——300之间");
        return;
    }
    
    axios.put("https://localhost:7231/api/House/UpdateHouse",data.value).then(res=>{
        if(res.data>0){
            alert("修改成功")
            router.push({path:'/'});
        }else{
            alert("修改失败");
        }
    })
}
const back=()=>{
    router.push({
        path:'/',
    })
}

</script>

<style scoped>

</style>